<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      class MyPromise {
        static race(arr) {
          return new Promise((resolve, reject) => {
            arr.forEach((item) => {
              item.then(resolve, reject);
            });
          });
        }
      }

      let p1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(111);
          console.log("执行了1");
        }, 4000);
      });

      let p2 = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(222);
          console.log("执行了2");
        }, 3000);
      });

      let p3 = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(3333);
          console.log("执行了3");
          // reject("err");
        }, 1000);
      });

      MyPromise.race([p1, p2, p3]).then(
        (res) => {
          console.log(res);
        },
        (err) => {
          console.log(err);
        }
      );
    </script>
  </body>
</html>
